package com.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;


import com.common.exception.BizCodeEnume;
import com.gulimall.member.excetpion.PhoneExistExcetpion;
import com.gulimall.member.excetpion.UserNameExistException;
import com.gulimall.member.vo.MemberLoginVo;
import com.gulimall.member.vo.MemberRegistVo;
import com.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.service.MemberService;
import com.common.utils.PageUtils;
import com.common.utils.R;


/**
 * @author shushushu
 * @email 717776244@qq.com
 * @date 2020-11-23 15:46:54
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {

        MemberEntity memberEntity = memberService.login(socialUser);
        if (memberEntity != null) {
            return R.ok();
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getMsg());
        }


    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {

        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            return R.ok().setData(entity);
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXECPTION.getMsg());
        }
    }


    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo) {

        try {
            memberService.regist(vo);
        } catch (PhoneExistExcetpion e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXECPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXECPTION.getMsg());
        } catch (UserNameExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXECPTION.getCode(), BizCodeEnume.USER_EXIST_EXECPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
